package com.ulook.lookplanet.ui.square.topic;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.ulook.lookplanet.base.BaseViewModel2;
import com.ulook.lookplanet.data.model.BaseTopic;
import com.ulook.lookplanet.data.model.TCommunity;
import com.ulook.lookplanet.data.model.TTopic;
import com.ulook.lookplanet.data.repository.TopicRepository;
import com.ulook.lookplanet.ui.square.VArticlePreInfo;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.json.JacksonUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.ResponseCallback;

import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/4/21
 */
public class TopicViewModel extends BaseViewModel2 {

    private TopicRepository repository;
    private BaseTopic baseTopic;
    private MutableLiveData<List<VArticlePreInfo>> artInfos = new MutableLiveData<>();

    public TopicViewModel(){
        repository = TopicRepository.getInstance();
    }

    public void setBaseTopic(BaseTopic baseTopic){
        this.baseTopic = baseTopic;
    }
    public BaseTopic getBaseTopic(){
        return baseTopic;
    }
    public LiveData<List<VArticlePreInfo>> getArtInfos(){
        return artInfos;
    }

    public void getArticleData(int currUserId, int itemNum){

        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    List<VArticlePreInfo> articlePreInfos = JacksonUtil.json2list((String)res, VArticlePreInfo.class);
                    artInfos.setValue(articlePreInfos);
                } catch (Exception e) {
                    setToastText("获取动态解析数据失败");
                    ThrowException.print(e);
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText(failuer.getEmsg());
                ThrowException.print(failuer, failuer.getEmsg());
            }
        };
        if(baseTopic instanceof TCommunity){
            repository.getArticlesForComuId(baseTopic.getUid(), currUserId, itemNum, callback);
        }else if(baseTopic instanceof TTopic){
            repository.getArticlesForTopicId(baseTopic.getUid(), currUserId, itemNum, callback);
        }
    }
}
